﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.LineManager
{
    public partial class LineList : Form
    {
        public LineList()
        {
            InitializeComponent();
            this.dgLine.AutoGenerateColumns = false;
        }
        private ZsLineDAL dal = new ZsLineDAL();
        private Dictionary<int, string> dc = new Dictionary<int, string>();
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            LineEditing zle = new LineEditing();
            zle.IsNew = true;
            if (zle.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = this.dgLine.SelectedRows.Count;
            if (count < 0)
            {
                MessageBox.Show("请选择要编辑的行");
                return;
            }
            else {
                LineEditing le = new LineEditing();
                le.IsNew = false;
                le.EditingId = this.dgLine.SelectedRows[0].Cells["zid"].Value.ToString(); 
                if(le.ShowDialog()==DialogResult.OK){
                    LoadData();
                }
            }
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = this.dgLine.SelectedRows.Count;
            if (count < 0)
            {
                MessageBox.Show("请选中要删除的行");
                return;
            }
            else {
                string info = "确定要删除"+this.dgLine.SelectedRows[0].Cells["zsbh"].Value.ToString()+"么";
                if (MessageBox.Show(info, "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    if(dal.DelData(this.dgLine.SelectedRows[0].Cells["zid"].Value.ToString())==1){
                        MessageBox.Show("删除成功");
                        LoadData();
                    }
                }
                else {
                    MessageBox.Show("删除失败");

                }
            }
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void LineList_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainform.ll = null;
        }
        //窗口加载
        private void LineList_Load(object sender, EventArgs e)
        {
            dc.Add(0, "否");
            dc.Add(1, "是");

            LoadData();
        }
        //加载数据
        public void LoadData() {
            BindingSource bs = new BindingSource();
            bs.DataSource = dc;
            zslb.DataSource = bs;
            zslb.ValueMember = "key";
            zslb.DisplayMember = "value";

            string zsbh = this.tbname.Text.Trim();
            string zsmc = this.tbdesc.Text.Trim();
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("zsbh",zsbh);
            dic.Add("zsmc",zsmc);

            this.dgLine.DataSource = dal.ListAll(dic);


        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
